<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>任务维护</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="/node_modules/LobsterUIFrame/layuiadmin/layui/css/layui.css" rel="stylesheet" />
    <link href="/node_modules/LobsterUIFrame/layuiadmin/style/admin.css" rel="stylesheet" />
    <link href="/node_modules/LobsterUIFrame/layuiadmin/layui/css/modules/layui-icon-extend/iconfont.css"
        rel="stylesheet" />
    <script src="/node_modules/LobsterUIFrame/layuiadmin/layui/layui.js"></script>
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-input-inline">
                    <div class="layui-input-inline" style="width: 200px;">
                        <input type="text" name="taskName" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-input-inline">
                    <label class="layui-form-label">任务类型：</label>
                    <div class="layui-input-inline">
                        <select name="taskType">
                            <option value="-1">全部</option>
                            <option value="1">新手任务</option>
                            <option value="2">日常任务</option>
                            <option value="3">购买消费</option>
                            <option value="4">晒单评价</option>
                        </select>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-app-front-search"
                        id="btnSearch">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查找
                    </button>
                </div>
            </div>
            <div class="layui-card-body">
                <table id="datatable" lay-filter="datatable"></table>
            </div>
        </div>
    </div>
</body>

<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon  layui-icon-edit"></i>编辑</a> 
    {{# if(d.Valid==0) { }}
    <a class="layui-btn layui-btn-xs" lay-event="valid"><i class="layui-icon  layui-icon-edit"></i>启用</a> 
    {{# }else{ }} 
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="valid"><i class="layui-icon layui-icon-edit"></i>停用</a> 
    {{# } }} 
</script>
<script type="text/html" id="add">
    <a class="layui-btn layui-btn-sm" lay-event="add" id="add"><i class="layui-icon layui-icon-add-1"></i>新增任务</a>
</script>

</html>

<script>
    layui.config({
        base: '/node_modules/LobsterUIFrame/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    });

    layui.define(['index', 'layer', 'form', 'element', 'table', 'laydate'], function () {
        var layer = layui.layer;
        var form = layui.form;
        var element = layui.element;
        var table = layui.table;
        var $ = layui.$;
        var admin = layui.admin;
        var setter = layui.setter;
        var laydate = layui.laydate;

        var controller = {
            data: {
                table: null,
                tableoptions: {},
                layerindex: 0,
                selectdata: {},
                taskTypeList: [{
                    id: 1,
                    text: '新手任务'
                }, {
                    id: 2,
                    text: '日常任务'
                }, {
                    id: 3,
                    text: '购买消费'
                }, {
                    id: 4,
                    text: '评价晒单'
                }],
            },
            //初始化页面
            initpage: function () {
                this.initevent();
                this.loaddata();
            },
            getbaseurl: function (fun) {
                var url = "/lowcode/APP20210728143245471/member/?view=layui&fun=" + fun;
                return url;
            },
            //初始化事件
            initevent: function () {
                var self = this;
                //查找reload,通过条件，查询网格的table重载数据显示到界面
                form.on("submit(LAY-app-front-search)", function (data) {
                    self.loaddata();
                });
                //模板新增
                table.on("toolbar(datatable)", function (obj) {
                    window.selectdata = null;
                    window.taskTypeList = self.data.taskTypeList;
                    //新增
                    if (obj.event == "add") {
                        self.openEdit("新增任务");
                    }
                });
                //监听行工具事件
                table.on('tool(datatable)', function (obj) {  //tool是工具条事件名，table原始容器的属性
                    var data = obj.data;//当前行数据

                    var layEvent = obj.event;//获得lay-event的值
                    if (obj.event === "edit") {
                        window.selectdata = data;
                        window.taskTypeList = self.data.taskTypeList;
                        self.openEdit(data.TaskName + '编辑');
                    } else if (obj.event == "valid") {
                        var tip = "确认停用吗?"
                        if (data.Valid == 0) {
                            tip = "确认启用吗?"
                        }
                        if (data.Valid == 0)
                            data.Valid = 1;
                        else
                            data.Valid = 0 
                        layer.confirm(tip, { icon: 3, title: '提示' }, function (index) {
                            layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                            self.enable(data, () => {
                                self.loaddata();
                                layer.close(index);
                            })
                        })
                    }
                });

            },
            //加载订单列表
            loaddata: function () {
                var self = this;
                self.data.table = table.render({
                    elem: '#datatable',
                    toolbar: '#add',
                    where: {
                        taskName: $("[name='taskName']").val(),
                        taskType: $("[name='taskType']").val(),
                    },
                    url: self.getbaseurl("task.gettasklist"),
                    height: 'full-130',
                    cellMinWidth: 80,
                    size: 'sm',
                    page: true,
                    cols: [[
                        { width: "8%", field: 'TaskCode', title: '任务编号' },
                        { width: "12%", field: 'TaskName', title: '任务名称' },
                        {
                            width: "10%", title: '任务类型', templet: function (d) {
                                var filter = self.data.taskTypeList.filter(a => { return a.id == d.TaskType });
                                if (filter != null && filter.length > 0) {
                                    if (d.TaskType == 3) {
                                        return filter[0].text + "(" + (d.RuleType == 1 ? '按比例' : '按金额范围') + ")";
                                    } else {
                                        return filter[0].text;
                                    }
                                }

                            }
                        },
                        {
                            width: "8%", field: 'BonusPoints', title: '奖励积分', templet: function (d) {
                                if (d.TaskType == 3) {
                                    return '-';
                                } else {
                                    return d.BonusPoints;
                                }
                            }
                        },
                        {
                            width: "8%", field: 'BonusGrowth', title: '奖励成长值', templet: function (d) {
                                if (d.TaskType == 3) {
                                    return '-';
                                } else {
                                    return d.BonusGrowth;
                                }
                            }
                        },
                        {
                            width: "8%", field: 'BonusPointsLimit', title: '奖励积分上限', templet: function (d) {
                                if (d.TaskType == 2) {
                                    return d.BonusPointsLimit;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            width: "8%", field: 'BonusGrowthLimit', title: '奖励成长值上限', templet: function (d) {
                                if (d.TaskType == 2) {
                                    return d.BonusGrowthLimit;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            width: "8%", field: 'PointsPercent', title: '奖励积分比例', templet: function (d) {
                                if (d.TaskType == 3 && d.RuleType == 1) {
                                    return d.PointsPercent + ":1";
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            width: "8%", field: 'GrowthPercent', title: '奖励成长值比例', templet: function (d) {
                                if (d.TaskType == 3 && d.RuleType == 1) {
                                    return d.GrowthPercent + ":1";
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            width: "6%", field: 'Valid', title: '状态', templet: function (d) {
                                if (d.Valid == 0) {
                                    return "停用";
                                } else {
                                    return "启用";
                                }
                            }
                        },
                        { field: 'operation', title: '操作', toolbar: '#operation', width: '20%', fixed: 'right' }
                    ]]
                })
            },
            //编辑
            openEdit: function (title) {
                var self = this;
                layer.open({
                    type: 2
                    , title: title
                    , content: 'edit.html'
                    , btn: ['保存', '取消']
                    , maxmin: true
                    , area: ['100%', '100%']  //宽，高
                    , success: function (layero, index) {

                    }
                    , btn2: function (index, layero) {

                    }
                    , cancel: function () {

                    }
                    , yes: function (index, layero) {
                        var a = getResult()
                        if (a != null) {
                            self.data.listmodel = a;
                            layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
                            self.savetask(() => {
                                self.loaddata();
                                layer.close(index);
                            })
                        }
                    }
                });
            },
            //保存任务 
            savetask: function (succ) {
                var self = this;
                admin.req({
                    url: self.getbaseurl("task.savetask"),
                    method: "POST",
                    data: { data: JSON.stringify(self.data.listmodel) },
                    done: res => {
                        if (res.code == 0) {
                            layer.msg("保存成功!")
                            succ && succ();
                        }
                        else
                            layer.msg("保存失败!")
                    }
                });
            },
            //停启用
            enable: function (data, succ) {
                var self = this;
                admin.req({
                    url: self.getbaseurl("task.validtask"),
                    method: "POST",
                    data: data,
                    done: res => {
                        layer.msg("保存成功!")
                        succ && succ();
                    }
                });
            }
        }

        /*
        * 入口
        */
        controller.initpage();
    });

</script>